//
//  CustomerCell.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/11.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CustomerCell.h"
#import "ProgressView.h"
#import "config.h"
#import "UIImageView+WebCache.h"
#import "FTWCache.h"


@interface CustomerCell ()

@property (nonatomic,strong)ProgressView *proView;

@property (weak, nonatomic) IBOutlet UILabel *orderId;
@property (weak, nonatomic) IBOutlet UILabel *orderTime;

@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *subTitle;
@property (weak, nonatomic) IBOutlet UILabel *dolLab;
@property (weak, nonatomic) IBOutlet UILabel *rmbLab;
@property (weak, nonatomic) IBOutlet UIView *progressContenView;


@property (weak, nonatomic) IBOutlet UILabel *dayLabel;

@property (weak, nonatomic) IBOutlet UILabel *hourLabel;

@property (weak, nonatomic) IBOutlet UILabel *minuteLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;

@property (weak, nonatomic) IBOutlet UILabel *paymentTime;
@property (weak, nonatomic) IBOutlet UIView *paymentDj;

@property (nonatomic,copy)NSString *lockEndTime;


@end


@implementation CustomerCell
{
     dispatch_source_t _timer;
}




+(instancetype) cellWithTableView:(UITableView *)tableview{
    
    static NSString *ID = @"CustomerCell";
    CustomerCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomerCell" owner:nil options:nil]lastObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        
        cell.proView = [ProgressView creatView];
        [cell.proView setFrame:CGRectMake(0, 10, cell.progressContenView.frame.size.width, cell.progressContenView.frame.size.height-20)];
        [cell.progressContenView addSubview:cell.proView];
    }
    
    return cell;
}



+(CGFloat)getHight:(OrderData*)data withTableView:(UITableView*)tableView{
    
    
    return 292;
}





-(void)setData:(OrderData*)data{
    
    [self.img sd_setImageWithURL:[NSURL URLWithString:data.imgUrl] placeholderImage:[UIImage imageNamed:DEFAULT_IMG]];
    [self.orderId setText:data.orderCode];
    [self.orderTime setText:data.time];
    [self.title setText:data.title];
    [self.subTitle setText:data.subTitle];
    [self.dolLab setText:data.dol];
    [self.rmbLab setText:[NSString stringWithFormat:@"约: %@万",data.rbm]];
    
    [self.proView setPogressState:data.state];
    
    
    NSInteger state = data.masterState;
    
    if(state == 1010){
        [self hiddenPaymentTime];
    }else if(state == 1020){
        [self showPaymenTime];
    }else if(state > 1020 && state < 2010){
        if (state == 1050) {
            [self showPaymenTime];
        }else{
            [self hiddenPaymentTime];
        }
    }else if(state >= 2010 && state < 3010){
        if (state == 2010) {
            [self showPaymenTime];
        }else{
            [self hiddenPaymentTime];
        }
    }else if (state == 3010 || state == 3020){
        [self hiddenPaymentTime];
    }else if (state == 9020 || state == 9030){
        [self hiddenPaymentTime];
    }
    else{
        [self hiddenPaymentTime];
     }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
   
    NSString *endTime =  [self checkObjectForNull:data.lockEndTime];
    NSDate *endDate = [dateFormatter dateFromString:endTime];
    NSDate *currentDate = [NSDate date];
    
    NSTimeInterval timeInterval = [endDate timeIntervalSinceDate:currentDate];
    NSInteger dayNum = timeInterval/(24*60*60);
    NSInteger hourNum = (timeInterval-(dayNum*24*60*60))/(60*60);
    NSInteger minNum = (timeInterval-dayNum*24*60*60 - hourNum*60*60)/60;
    NSInteger senNum = timeInterval-dayNum*24*60*60 - hourNum*60*60 - minNum*60;
    
    self.dayLabel.text = [NSString stringWithFormat:@"%.2ld",(long)dayNum];
    self.hourLabel.text = [NSString stringWithFormat:@"%.2ld",(long)hourNum];
    self.minuteLabel.text = [NSString stringWithFormat:@"%.2ld",(long)minNum];
    self.secondLabel.text = [NSString stringWithFormat:@"%.2ld",(long)senNum];
    [self startUpdateTimer];
    
    self.lockEndTime = data.lockEndTime;

}

#pragma mark - Timer Methods
- (void)startUpdateTimer
{
    if (_timer) {
        return;
    };
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            //2014-04-01 12:00:00
            NSCalendar *calendar = [NSCalendar currentCalendar];
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
            NSDate *fireDate=[dateFormatter dateFromString:self.lockEndTime];
            fireDate = [FTWCache getLocData:fireDate];
            
            NSDate *today = [NSDate date];//当前时间
            today = [FTWCache getLocData:today];
            
            
            unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *d = [calendar components:unitFlags fromDate:today toDate:fireDate options:0];//计算时间差
            
            
            NSInteger day = [d day];
            NSInteger hour = [d hour];
            NSInteger minute = [d minute];
            NSInteger second = [d second];
            
            self.dayLabel.text = [NSString stringWithFormat:@"%.2ld ",(long)day];
            self.hourLabel.text = [NSString stringWithFormat:@"%.2ld",(long)hour];
            self.minuteLabel.text = [NSString stringWithFormat:@"%.2ld",(long)minute];
            self.secondLabel.text = [NSString stringWithFormat:@"%.2ld",(long)second];

            
        });
    });
    dispatch_resume(_timer);
}

- (void)cancelTimer
{
    dispatch_source_cancel(_timer);
}

- (void)hiddenPaymentTime
{
    self.paymentDj.hidden = YES;
    self.paymentTime.hidden = YES;
}
- (void)showPaymenTime
{
    self.paymentDj.hidden = NO;
    self.paymentTime.hidden = NO;
}

- (NSString*)checkObjectForNull:(NSString*)objectValue
{
    if(objectValue==nil || objectValue ==(id)[NSNull null])
    {
        return @"";
    }
    else
    {
        return objectValue;
    }
}



@end
